package com.level.system.task;

import com.alibaba.fastjson.JSON;
import com.level.system.entity.emr.DATA_HZ;
import com.level.system.service.EmrParseService;
import com.level.system.util.EmrParserUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.*;

public class HZ_SWJLThread extends Thread {


    private List<DATA_HZ> dsList;
    private EmrParseService emrService;

    public HZ_SWJLThread(List<DATA_HZ> list, EmrParseService service) {
        this.dsList = list;
        this.emrService = service;
    }

    /**
     * 获取元素内容
     *
     * @param element
     * @return
     */
    private static String getMRXmlText(Element element) {
        String levelchapter = element.attr("levelchapter");
        Map<String, Object> chapterMap = JSON.parseObject(levelchapter);
        // 获取默认值属性
        String chapterId = chapterMap.get("chapterId").toString();
        // 1移除levelplugins=nodeblock
        element.select("span[levelplugins='nodeblock']").remove();
        // 2提取leftbrackets、rightbrackets节点内容

        // 替换空格
        String tempHtml = element.html();
        tempHtml = tempHtml.replaceAll("&nbsp;", " ");
        element.html(tempHtml);

        String text = element.text().trim();
        // System.out.println("element.html " + element.html());
        if (text.length() > 0) {
            // 去掉[
            text = text.substring(1, text.length());
            // 去掉]
            if (text.length() > 0) {
                text = text.substring(0, text.length() - 1);
            }
        }
        return text.trim();
    }

    @Override
    public void run() {
        handleTask(this.dsList);
    }

    private void handleTask(List<DATA_HZ> list) {
        List<Map<String, Object>> paramList = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramMap = new HashMap<String, Object>();

        for (Iterator it = list.iterator(); it.hasNext(); ) {
            // 1获取病历对象
            DATA_HZ emrParser = (DATA_HZ) it.next();
            // 2获取病历html
            String html = new String(emrParser.getMRCONTENT());
            // 3获取病历解析结果
            Map<String, Object> map = getSwjl(html);
            map.put("jgid", emrParser.getJGID());
            map.put("id", emrParser.getID());
            map.put("mcNo", emrParser.getMCNO());
            map.put("docId", emrParser.getDOCID());
            map.put("mzzy", emrParser.getMZZY());
//            map.put("emrType", emrParser.getEMRTYPE());
            map.put("archiveTime", emrParser.getARCHIVETIME());
            map.put("rysj", emrParser.getRYSJ());
            map.put("cysj", emrParser.getCYSJ());

            map.put("name", emrParser.getNAME());
            map.put("sex", emrParser.getSEX());
            map.put("birthday", emrParser.getBIRTHDAY());

            map.put("creater", "dba");
            map.put("createTime", new Date());


            map.put("emrType", "9");
            paramList.add(map);
        }
        paramMap.put("list", paramList);
        emrService.batchSaveCyxj(paramMap);
    }

    /**
     * 获取死亡记录
     *
     * @param html
     * @return
     */
    public Map<String, Object> getSwjl(String html) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("state", "0");

        // 病历文书内容
        Document doc = Jsoup.parseBodyFragment(html);
        // 获取所有符合条件的div标签域
        Elements lcs = doc.select("span[levelchapter]");
        for (Iterator it = lcs.iterator(); it.hasNext(); ) {
            Element lc = (Element) it.next();
            String levelchapter = lc.attr("levelchapter");
            // 转成map对象
            Map<String, Object> chapterMap = JSON.parseObject(levelchapter);
            // 获取默认值属性
            String chapterId = chapterMap.get("chapterId").toString();
            if ("MR-1001-1008-1006".equals(chapterId)) {
                // 入院情况
                map.put("ryqk", EmrParserUtil.subStr(getMRXmlText(lc), 4000));
            } else if ("MR-1001-1008-1008".equals(chapterId)) {
                // 诊疗过程
                map.put("zlgc", EmrParserUtil.subStr(getMRXmlText(lc), 4000));
            } else if ("MR-1001-1008-1007".equals(chapterId)) {
                // 出院情况
                map.put("cyqk", EmrParserUtil.subStr(getMRXmlText(lc), 4000));
            } else if ("MR-1001-1008-1004".equals(chapterId)) {
                // 出院医嘱
                map.put("cyyz", EmrParserUtil.subStr(getMRXmlText(lc), 4000));
            }
        }
        return map;
    }
}
